package com.project.forlearning.service.impl;

import com.alibaba.fastjson.JSON;
import com.project.forlearning.constant.ResponseTool;
import com.project.forlearning.dao.BackUserResourceMapper;
import com.project.forlearning.pojo.Resource;
import com.project.forlearning.pojo.vo.ResponseInfo;
import com.project.forlearning.service.BackUserResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
public class BackUserResourceServiceImpl implements BackUserResourceService {

    @Autowired
    private BackUserResourceMapper backUserResourceMapper;

    /**
     * 查询某一个用户的某一个收藏资源
     * @param resourceId
     * @param userId
     * @return
     */
    @Override
    public String findUserResource(int resourceId, int userId) {
        if(resourceId <= 0 || userId <= 0){
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DATA_ERROR.code, ResponseTool.DATA_ERROR.msg, false));
        }

        Resource resource = backUserResourceMapper.findUserResource(resourceId, userId);

        if(resource != null){
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS.code, ResponseTool.QUERY_SUCCESS.msg, resource));
        }
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_FAIL.code,ResponseTool.QUERY_FAIL.msg, false));
    }

    /**
     * 查询某一个用户的所有收藏资源
     * @param userId
     * @return
     */
    @Override
    public String findUserAllResource(int userId) {

        if(userId <= 0)
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DATA_ERROR.code, ResponseTool.DATA_ERROR.msg, false));

        List<Resource> resourceList = backUserResourceMapper.findUserAllResource(userId);

        //若查询结果为空
        if(resourceList == null){
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS_ALL.code, ResponseTool.QUERY_SUCCESS_ALL.msg, false));
        }
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS.code, ResponseTool.QUERY_SUCCESS.msg, resourceList));
    }

    /**
     * 删除某一个用户的某一个收藏资源
     * @param resourceId
     * @param userId
     * @return
     */
    @Override
    public String deleteUserResource(int resourceId, int userId) {
        //参数校验
        if(resourceId <= 0 || userId <= 0)
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DATA_ERROR.code, ResponseTool.DATA_ERROR.msg, false));

        //删除成功
        if(backUserResourceMapper.deleteUserResource(resourceId,userId)){
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_SUCCESS.code, ResponseTool.DELETE_SUCCESS.msg, true));
        }
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_FAIL.code, ResponseTool.DELETE_FAIL.msg, false));
    }

}
